//
//  NewFeatureCell.swift
//  kingWeiBo
//
//  Created by King on 15/9/14.
//  Copyright © 2015年 King. All rights reserved.
//

import UIKit


class NewFeatureCell: UICollectionViewCell {
    
    func clickStartButton(){
    
        NSNotificationCenter.defaultCenter().postNotificationName(KNTongzhi, object: true)
    }
    
    var imageIndex: Int = 0{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
        
    }
    
    func startButtonAnimation(){
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
                self.startButton.userInteractionEnabled = true
        }
        
    }
    
/// 最后一个页面播放动画
    
        
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        

    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    func setUpUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
            /// 设置约束
            iconView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": iconView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": iconView]))

        
            iconView.frame.size = UIScreen.mainScreen().bounds.size
        
        /// 开始体验按钮约束
        startButton.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 200))
        
    }
  
    private lazy var iconView = UIImageView()
    private lazy var startButton:UIButton = {
        let startBtn = UIButton()
        startBtn.setTitle("开始体验", forState: UIControlState.Normal)
        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"),
                                    forState: UIControlState.Normal)
        startBtn.setBackgroundImage(UIImage(named:
                                    "new_feature_finish_button_highlighted"),
                                    forState: UIControlState.Highlighted)
        startBtn.sizeToFit()
        startBtn.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        return startBtn
    }()
    
    
    
    
}

class  KNlayout:UICollectionViewFlowLayout{
    
    override func prepareLayout() {
        itemSize = collectionView!.bounds.size
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
}

